import java.util.Arrays;

public class Strings {

    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder();
        System.out.println((int) '0');
        int a = '0' + '2';
        System.out.println("a "+a);
        System.out.println((int) '1');
        System.out.println((int) '1' * 2);
        System.out.println((int) '1' + (int) '1' + (int) '1');
        System.out.println((int) '1' + (int) '0' + (int) '0');
        System.out.println((int) '1' + (int) '0' + (int) '1');
        System.out.println((int) '0' + (int) '0' + (int) '1');
        System.out.println((int) '0' + (int) '0' + (int) '0');

        System.out.println('1' - '0');

    }

    /**
     * 字符串相乘
     */

    //先乘 后进位
    public String multiplyBe(String num1, String num2) {
        StringBuilder n1 = new StringBuilder(num1);
        StringBuilder n2 = new StringBuilder(num2);

        char[] c1 = n1.reverse().toString().toCharArray();
        char[] c2 = n2.reverse().toString().toCharArray();

        int n = c1.length;
        int m = c2.length;
        if(c1[n-1] - '0' == 0 || c2[m-1] - '0' == 0) return "0";

        int[] arr = new int[m + n - 1];

        //先乘不进位
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                int v1 = c1[j] - '0';
                int v2 = c2[i] - '0';
                arr[i+j]+=v1*v2;
            }
        }
        StringBuilder ret = new StringBuilder();

        int carry = 0;

        for(int i : arr){
            ret.append((i + carry) % 10);
            carry = (i + carry) / 10;
        }

        if(carry != 0) ret.append(carry);

        return ret.reverse().toString();
    }

//     模拟
    public String multiply(String num1, String num2) {
        //写一个相加和相乘的
        char[] c1 = num1.toCharArray();
        char[] c2 = num2.toCharArray();

        int n = c1.length;
        int m = c2.length;

        if(c1[0] - '0' == 0 || c2[0] - '0' == 0) return "0";

        // c1 * c2
        int pow = 0;
        String ret = "0";
        for(int i = m - 1; i >= 0 ;i--){
            String n1 = mul(c1, c2[i], pow);
            ret = strAdd(ret, n1);
            pow++;
        }
        return ret;
    }

    String strAdd(String n1, String n2){
        char[] c1 = n1.toCharArray();
        char[] c2 = n2.toCharArray();
        int n = c1.length - 1;
        int m = c2.length - 1;

        int carry = 0;
        StringBuilder str = new StringBuilder("");
        while(n >= 0 || m >= 0){
            int v1 = 0; int v2 = 0;
            if(n >= 0){
                v1 = c1[n--] - '0';
            }
            if(m >= 0){
                v2 = c2[m--] - '0';
            }
            str.append((v1 + v2 + carry) % 10);
            carry = (v1 + v2 + carry) / 10;
        }
        if(carry != 0) str.append(carry);
        // System.out.println("add n1 = "+ n1 + " n2 = "+ n2 +" v = " + str.reverse().toString());
        return str.reverse().toString();
    }

    String mul(char[] c1, char v, int pow){
        StringBuilder str = new StringBuilder("");
        int n = c1.length;
        for(int i = 0; i < pow; i++){
            str.append("0");
        }

        // c1 * v
        int carry = 0;
        int v1 = v - '0';
        for(int i = n - 1; i >= 0; i--){
            int n1 = c1[i] -'0';
            int add = (n1 * v1 + carry) % 10;
            carry = (n1 * v1 + carry)/ 10;
            str.append(add);
        }
        if(carry != 0) str.append(carry);
        // System.out.println("c1 = "+ Arrays.toString(c1) + " v = "+ v + " pow v = " + str.reverse().toString() + "\n");
        return str.reverse().toString();
    }
    /**
     * 二进制求和
     */
    public String addBinary(String a, String b) {
        char[] aa = a.toCharArray();
        char[] bb = b.toCharArray();
        int i = a.length() - 1;
        int j = b.length() - 1;

        StringBuilder ret = new StringBuilder();
        char buff = '0';
        while(i >= 0 && j >= 0){
            int v = aa[i] + bb[j] + buff;
            buff = '0';
            if(v == 144) ret.append("0");
            else if(v == 145) ret.append("1");
            else if(v == 146){
                buff = '1';
                ret.append("0");
            }else{
                buff = '1';
                ret.append("1");
            }
            i--;j--;
        }
        while(i >= 0){
            int v = aa[i] + buff;
            buff = '0';
            if(v == 96) ret.append("0");
            else if(v == 97) ret.append("1");
            else{
                buff = '1';
                ret.append("0");
            }
            i--;
        }

        while(j >= 0){
            int v = bb[j] + buff;
            buff = '0';
            if(v == 96) ret.append("0");
            else if(v == 97) ret.append("1");
            else{
                buff = '1';
                ret.append("0");
            }
            j--;
        }
        if(buff == '1') ret.append("1");
        return ret.reverse().toString();
    }
    /**
     * 最长的回文子串
     * 中心扩展算法
     */
    public String longestPalindrome(String s) {
        int n = s.length();
        char[] ss = s.toCharArray();
        int max = -1;
        int[] index = new int[2];
        int l = 0, r = 0;
        for (int i = 0; i < n; i++) {
            //奇数情况
            l = i - 1;
            r = i + 1;
            while (l >= 0 && r < n && ss[l] == ss[r]) {
                l--;
                r++;
            }
            if (max < r - l) {
                max = r - l;
                index[0] = l;
                index[1] = r;
            }
            //偶数情况
            l = i;
            r = i + 1;
            while (l >= 0 && r < n && ss[l] == ss[r]) {
                l--;
                r++;
            }
            if (max < r - l) {
                max = r - l;
                index[0] = l;
                index[1] = r;
            }
        }
        return s.substring(index[0] + 1, index[1]);
    }
}
